package by.dvrudenko.avtobaza.webapp.page.car;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.dvrudenko.avtobaza.datamodel.Car;
import by.dvrudenko.avtobaza.datamodel.CarType;
import by.dvrudenko.avtobaza.datamodel.enums.FuelType;
import by.dvrudenko.avtobaza.services.CarService;
import by.dvrudenko.avtobaza.services.CarTypeService;
import by.dvrudenko.avtobaza.webapp.page.BaseLayout;
import by.dvrudenko.avtobaza.webapp.page.home.HomePage;
import by.dvrudenko.avtobaza.webapp.utils.datepicker.JQueryDateField;
import by.dvrudenko.avtobaza.webapp.utils.renderer.CarTypeChoiseRenderer;

@AuthorizeInstantiation({ "ADMIN" })
public class CreateCarPage extends BaseLayout {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(CreateCarPage.class);

	@Inject
	private CarService carService;
	
	@Inject
	private CarTypeService carTypeService;	
	
	private Date date = new Date();
	
	// типы топлива
	final List<FuelType> listFuelType = Arrays.asList(FuelType.values());
	// все типы машин из CarType
	final List<CarType> listCarType = carTypeService.getAllCarType();
	
	public CreateCarPage(final Car car) {
		super();
		Form<Car> formCreateCar = new Form<Car>("form", new CompoundPropertyModel<Car>(car));
					
		final TextField<Date> carYear = new JQueryDateField("date" , new PropertyModel<Date>(this, "date"));
		formCreateCar.add(carYear);
						
		final Label carConditionLabel = new Label("carConditionLabel" , new ResourceModel("p.home.car.createcar.carConditionLabelWork"));
		formCreateCar.add(carConditionLabel);
		carConditionLabel.setOutputMarkupId(true);
		
		// чек бокс с состоянием машины
		final AjaxCheckBox checkBoxCarCondition =new AjaxCheckBox("carCondition"){
			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				if (car.getCarCondition()){
					carConditionLabel.setDefaultModelObject(new ResourceModel("p.home.car.createcar.carConditionLabelWork").getObject());
				}else {
					carConditionLabel.setDefaultModelObject(new ResourceModel("p.home.car.createcar.carConditionLabelNotWork").getObject());	
				}
				target.add(carConditionLabel);			
			}
		};	
		checkBoxCarCondition.setOutputMarkupId(true);			
		formCreateCar.add(checkBoxCarCondition);		
		checkBoxCarCondition.setDefaultModelObject(true);
				
	
		final Label workLabel = new Label("workLabel" , new ResourceModel("p.home.car.createcar.workLabelInBox"));
		workLabel.setOutputMarkupId(true);
		formCreateCar.add(workLabel);
		
		// чек бокс с машиной где находится? на задании или в гараже
		final AjaxCheckBox checkBoxCarWork =new AjaxCheckBox("work"){
			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				if (car.getWork()){
					workLabel.setDefaultModelObject(new ResourceModel("p.home.car.createcar.workLabelInRoute").getObject());
				}else {
					workLabel.setDefaultModelObject(new ResourceModel("p.home.car.createcar.workLabelInBox").getObject());	
				}
				target.add(workLabel);			
			}
		};	
		checkBoxCarWork.setOutputMarkupId(true);			
		formCreateCar.add(checkBoxCarWork);		
				
		final TextField<Integer> carCarryingCapacity = new TextField<Integer>("carryingCapacity");
		carCarryingCapacity.setLabel(new ResourceModel("p.home.car.panel.carryingCapacity"));
		carCarryingCapacity.add(new PropertyValidator<Integer>());
		formCreateCar.add(carCarryingCapacity);
		
		final TextField<Integer> carVolumeCapacity = new TextField<Integer>("volumeCapacity");
		carVolumeCapacity.setLabel(new ResourceModel("p.home.car.panel.volumeCapacity"));		
		carVolumeCapacity.add(new PropertyValidator<Integer>());		
		formCreateCar.add(carVolumeCapacity);
		
		formCreateCar.add(new DropDownChoice<FuelType>("fuelType", listFuelType).setRequired(true).
				setLabel(new ResourceModel("p.home.car.createcar.fuelType")));

	
		final TextField<Integer> carNumberOfSeats = new TextField<Integer>("numberOfSeats");
		carNumberOfSeats.setLabel(new ResourceModel("p.home.car.createcar.numberOfSeats"));			
		carNumberOfSeats.add(new PropertyValidator<Integer>());		
		formCreateCar.add(carNumberOfSeats);		
		
		final TextField<Integer> carBodyLength = new TextField<Integer>("bodyLength");
		carBodyLength.setLabel(new ResourceModel("p.home.car.createcar.bodyLength"));			
		carBodyLength.add(new PropertyValidator<Integer>());		
		formCreateCar.add(carBodyLength);	
		
		final TextField<Integer> carBodyWidth = new TextField<Integer>("bodyWidth");
		carBodyWidth.setLabel(new ResourceModel("p.home.car.createcar.bodyWidth"));		
		carBodyWidth.add(new PropertyValidator<Integer>());	
		formCreateCar.add(carBodyWidth);	
		
		final TextField<Integer> carNumber = new TextField<Integer>("carNumber");
		carNumber.setLabel(new ResourceModel("p.home.car.createcar.carNumber"));			
		carNumber.add(new PropertyValidator<Integer>());
		formCreateCar.add(carNumber);			
		
		// комбик с типами машин
		formCreateCar.add(new DropDownChoice<CarType>("carType", listCarType, 
				CarTypeChoiseRenderer.INSTANCE).setRequired(true).
				setLabel(new ResourceModel("p.home.car.createcar.carType")));

		
		// сохраняем машину
		formCreateCar.add(new SubmitLink("sumbit-link") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				car.setYear(convertDateToTimestamp(carYear));
				carService.saveOrUpdate(car);
				LOGGER.info("User create Car" + user.getLogin());
				HomePage hp = new HomePage();
				hp.info(new ResourceModel("p.home.createCarSubmit").getObject());
				setResponsePage(hp);
			}
		});
		
		add(formCreateCar);
	}



}
